package com.example.bookstore.api

import com.example.bookstore.dao.BaseDao
import com.example.bookstore.entity.BaseEntity
import com.example.bookstore.service.BaseService
import com.example.bookstore.validate.PageNSizeValidate
import io.swagger.annotations.ApiImplicitParam
import io.swagger.annotations.ApiImplicitParams
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestBody
import javax.validation.constraints.NotBlank

/**
 * Created by IntelliJ IDEA.
 * User: lvshe
 * Date: 2019/8/10
 * Time: 13:06
 */
@Validated
@Suppress("SpringJavaAutowiredMembersInspection", "SpringJavaInjectionPointsAutowiringInspection")
open class BaseController<SERVICE : BaseService<DAO, E>, DAO : BaseDao<E>, E : BaseEntity> {
    @Autowired
    lateinit var service: SERVICE

    @ApiOperation("【测试专用】【继承于基类】获取列表", notes = "【基类控制器】获取列表，仅测试使用，生产环境清继承配置使用或进行实现")
    @ApiImplicitParams(
            ApiImplicitParam("页数 (0-2147483647）", name = "page", defaultValue = "0", allowableValues = "range(0, 10)", required = false),
            ApiImplicitParam("每页显示个数 （0-1000）", name = "size", defaultValue = "100", allowableValues = "range(0, 10)", required = false)
    )
//    @GetMapping("/list")
    open fun getAllPageable(@Validated pageNSizeValidate: PageNSizeValidate): MutableList<E> {
        return service.getAllPageable(pageNSizeValidate.page, pageNSizeValidate.size)
    }

    @ApiOperation("【测试专用】【继承于基类】获取该ID下的信息", notes = "【基类控制器】获取该ID下的信息，仅测试使用，生产环境清继承配置使用或进行实现")
    @ApiImplicitParams(ApiImplicitParam("实体类的ID", name = "id", required = true))
//    @GetMapping("/{id}")
    open fun getById(@NotBlank(message = "id 不能为空") @PathVariable id: String?): E {
        return service.getById(id!!)
    }

    @ApiOperation("【测试专用】【继承于基类】删除该ID下的信息", notes = "【基类控制器】删除该ID下的信息，仅测试使用，生产环境清继承配置使用或进行实现")
    @ApiImplicitParams(ApiImplicitParam("实体类的ID", name = "id", required = true))
//    @DeleteMapping("/{id}")
    open fun deleteById(@NotBlank(message = "id 不能为空") @PathVariable id: String?) {
        return service.deleteById(id!!)
    }

    @ApiOperation("【测试专用】【继承于基类】更新该ID下的信息", notes = "【基类控制器】更新该ID下的信息，接受json格式信息进行更新，仅测试使用，生产环境清继承配置使用或进行实现")
    @ApiImplicitParams(ApiImplicitParam("实体类的ID", name = "id", required = true))
//    @PutMapping("/{id}")
    open fun update(@NotBlank(message = "id 不能为空") @PathVariable id: String?, @RequestBody json: String): E {
        return service.updateWithJson(id!!, json)
    }

}